package com.dzq.utils
{
	import flash.display.BitmapData;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * @author Ethan.Du http://www.duzengqiang.com
	 * @date 2008-05-19
	 */
public class BitmapUtil
{
	public static function rotateLeft(bitmapdata:BitmapData):BitmapData{
	var matrix:Matrix = new Matrix();
   matrix.rotate(-1*Math.PI/2);
   matrix.ty = bitmapdata.width; 
   var bd:BitmapData = new BitmapData(bitmapdata.height, bitmapdata.width);
   	bd.draw(bitmapdata, matrix);
   	return bd; 
	}
	
	public static function rotateRight(bitmapdata:BitmapData):BitmapData{
		var matrix:Matrix = new Matrix();
	  	matrix.rotate(Math.PI/2);
		matrix.tx = bitmapdata.height; 
	   	var bd:BitmapData = new BitmapData(bitmapdata.height, bitmapdata.width);
   		bd.draw(bitmapdata, matrix);
   		return bd; 
	}
	
	
	public static function flipLR(bitmapdata:BitmapData):BitmapData{
		var matrix:Matrix = new Matrix();
	  	matrix.scale( -1, 1);
	  	matrix.translate( bitmapdata.width, 0);
   		var bd:BitmapData = new BitmapData(bitmapdata.width, bitmapdata.height);
   		bd.draw(bitmapdata, matrix);
   		return bd; 
	}
	
	public static function flipTB(bitmapdata:BitmapData):BitmapData{
		var matrix:Matrix = new Matrix();
	  	matrix.scale( 1, -1);
	  	matrix.translate( 0, bitmapdata.height);
   		var bd:BitmapData = new BitmapData(bitmapdata.width, bitmapdata.height);
   		bd.draw(bitmapdata, matrix);
   		return bd; 
	}
	
	public static function crop(bitmapdata:BitmapData, x:Number, y:Number, width:Number, height:Number):BitmapData{
		var bd:BitmapData = new BitmapData(width, height);
		bd.copyPixels( bitmapdata, new Rectangle(x, y, width, height), new Point(0,0));
		return bd;
	}
	
	public static function scale( bitmapdata:BitmapData, sx:Number):BitmapData{
		var bd:BitmapData = new BitmapData(bitmapdata.width * sx, bitmapdata.height * sx );
		var matrix:Matrix = new Matrix();
		matrix.scale(sx, sx);
		bd.draw(bitmapdata, matrix);
		return bd;
	}
	
	public static function resize(bitmapdata:BitmapData, width:Number, height:Number):BitmapData{
		var ratio:Number = (bitmapdata.width / bitmapdata.height > width / height)?
							(width / bitmapdata.width):
							(height / bitmapdata.height); 
		return scale(bitmapdata, ratio);
	}
	public static function CopyPixels(source:BitmapData,
									startX:int, startY:int,
									width:int, height:int,
									flipV:Boolean=false,flipH:Boolean=false):BitmapData {
		var nbd:BitmapData = new BitmapData(width,height,false,0xFFFFFF);
		source.lock();
		nbd.lock();
	
		var hy:int=0;
		var hx:int=0;
	
		var sx:int=0;
		for (var i:int=startX; i<startX+width; i++) {
			var sy:int=0;
			for (var j:int=startY; j<startY+height; j++) {
				if (flipV) {
					hy = height-sy-1;
				} else {
					hy = sy;
				}
				if (flipH) {
					hx = width-sx-1;
				} else {
					hx = sx;
				}
				nbd.setPixel32(hx,hy,source.getPixel32(i,j));
				sy++;
			}
			sx++;
		}
		source.unlock();
		nbd.unlock();
	
		return nbd;
	}

	}
}